//
//  TF_RecommendView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/3/30.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_RecommendView: UIView, UITableViewDelegate ,UITableViewDataSource {

    let imageView = UIImageView()
    let title = UILabel()
    let close = UIButton()
    //定时器
    var timer:Timer?
    let list = NSMutableArray()
    let tableView = UITableView()
    let glView = UIView()
    var gl = CAGradientLayer()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.clipsToBounds = true
        

        self.addSubview(self.glView)
  
        self.gl.startPoint = CGPoint(x: 0.5, y: 0)
        self.gl.endPoint = CGPoint(x: 0.5, y: 1)
        self.glView.layer.addSublayer(self.gl)
      

        self.title.numberOfLines = 0;
        self.addSubview(self.title)

        
        let titleColor = UIColor(red: 242/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1.0)

        self.title.attributedText = NSMutableAttributedString.getAttributed(string: "主播推荐宝贝，快来抢吧！", font: UIFont.PingFang(size: 13), foregroundColor: titleColor)
        
        self.title.textColor = titleColor
        self.title.textAlignment = .left
        
        //TODO:关闭推荐
        self.close.setImage(UIImage.init(named: "HTLive.bundle/closetuijian"), for: .normal)

        self.addSubview(self.close)
        self.close.addTarget(self, action: #selector(closeClick), for: .touchUpInside)

        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.addSubview(self.tableView)
        self.tableView.separatorStyle = .none;
        

        self.imageView.image = UIImage.init(named: "HTLive.bundle/aixintuijian")
        self.imageView.contentMode = .scaleAspectFit
        self.addSubview(self.imageView)
        
        self.tableView.register(TF_RecommendViewCell.self, forCellReuseIdentifier: "TF_RecommendViewCell")
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.layer.cornerRadius = 6;
        
        self.glView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 25 + 6)

        self.gl.frame = self.glView.bounds
        self.gl.colors = [UIColor(red: 255/255.0, green: 123/255.0, blue: 123/255.0, alpha: 1.0).cgColor, UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.3).cgColor]
        self.gl.locations = [0,0.7,1];
        self.glView.layer.cornerRadius = 12
        
        self.imageView.frame = CGRect(x: 3, y: 0, width: 19.62 * 2, height: 14.69 * 2)

        
        self.title.frame = CGRect(x: 42, y: 8 + 3, width: 156, height: 18)
     

        self.close.frame = CGRect(x: self.frame.size.width - 18 - 5, y: 8 + 3, width: 18, height: 18)

    }
    @objc func closeClick()
    {
         let Hehght:CGFloat = self.frame.size.height;
         let width:CGFloat = self.frame.size.width;
         let Y:CGFloat = self.frame.origin.y
 
      
        self.frame = CGRect(x: 0, y: Y, width: width, height: Hehght)
        UIView.animate(withDuration: 0.5) {
            self.frame = CGRect(x: -width, y: Y, width: width, height: Hehght)
        }

        
    }
   @objc public func destroy()
    {
        self.timeEnd()
    }

    func timeEnd()
    {
        if((self.timer) != nil)
        {
            self.timer?.invalidate()
            self.timer = nil
           
        }
    }
    @objc func timerAction()
    {
        DispatchQueue.main.async {
            self.timeEnd()
            self.closeClick()
        }
  
    }
    
    @objc public func addUpdate(list:NSArray)
    {
    
        var isUpdate:Bool = false
        if (list.count > self.list.count) {
            isUpdate = true
        }
        self.list.removeAllObjects()
        self.list.addObjects(from: list as! [Any])
        
        print("list.count====>",list.count)

        self.timeEnd()
        self.timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector:#selector(timerAction), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)

       
        
       let Hehght:CGFloat = 25 + ( (8 + 61 + 8 ) * CGFloat(list.count) ) + 6
       let width:CGFloat = 230
        
        let Y:CGFloat = self.superview!.frame.size.height - Hehght - 57
        if (isUpdate) {
            self.frame = CGRect(x: -width, y: Y, width: width, height: Hehght)
        }
        let tableViewY:CGFloat = 25 + 6
        let tableViewWidth:CGFloat = self.frame.size.width
        let tableViewHeight:CGFloat = self.frame.size.height - tableViewY
        self.tableView.frame = CGRect(x: 0, y: tableViewY, width: tableViewWidth, height: tableViewHeight)

        self.tableView.reloadData()
        if (isUpdate) {
            UIView.animate(withDuration: 0.5) {

            self.frame = CGRect(x: 0, y: Y, width: width, height: Hehght)

            }
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_RecommendViewCell") as! TF_RecommendViewCell
        cell.selectionStyle = .none
        let model = self.list[indexPath.row] as! TFgoods
         cell.cellWidth = self.frame.size.width;
        
        cell.model = model
        return cell
    }
    // 设置cell高度
       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
           return  8 + 61 + 8 
       }
    // 选中cell后执行此方法
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         
         //取出选中的cell
         let cell = tableView.cellForRow(at: indexPath) as! TF_RecommendViewCell
   
          let model = self.list[indexPath.row] as! TFgoods
         let qrcode:NSString = model.qrcode as NSString
         if (qrcode.length > 0) {
             TalkfunSharedTools.openUrl(image: cell.QC.image!)
             
         }
          
     }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
